    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpSession;
import model.Gebruiker;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author woutermkievit
 */
public class MenuHandler {
    private HttpSession sessie;
    public MenuHandler(HttpSession sessie){
         this.sessie = sessie;
         fillmenu();
    }
    
    List<String> menu = new ArrayList();
    List<String> menuAdmin = new ArrayList();
    
    public void menuAdd(String string) {
        string="<a href='Menu?redirection="+string+"'>"+string+"</a>";
        if(!menu.contains(string)){
            menu.add(string);
        }
        saveMenu();
    }
    public void menuAdd(String string,String display) {
        string="<a href='Menu?redirection="+string+"'>"+display+"</a>";
        if(!menu.contains(string)){
            menu.add(string);
        }
        saveMenu();
    }
    public void menuAddAdmin(String string) {
        string="<a href='Menu?redirection="+string+"'>"+string+"</a>";
        if(!menuAdmin.contains(string)){
            menuAdmin.add(string);
        }
        saveMenu();
    }
    public void menuAddAdmin(String link,String display) {
        System.out.println("blablabla");
        String string="<a href='Menu?redirection="+link+"' >"+display+"</a>";
        if(!menuAdmin.contains(string)){
            menuAdmin.add(string);
        }
        saveMenu();
    }
    public void menuAddNewP(String link,String display) {
        String string="<a href='"+link+"' target='_blank'>"+display+"</a>";
        if(!menu.contains(string)){
            menu.add(string);
        }
        saveMenu();
    }
    
    public void menuAddS(String string) {
        if(!menu.contains(string)){
            menu.add(string);
        }
        saveMenu();
    }
    public void menuRemove(String string) {
        
        string="<a href='Menu?redirection="+string+"'>"+string+"</a>";
        if(menu.contains(string)){
            menu.remove(string);
        }
        saveMenu();
    }
    public void clear(){
        menu.clear();
        menuAdmin.clear();
        saveMenu();
    }
    public String toString(List<String> menu) {
        Session session = SchoolContentManager.getSessionFactory().getCurrentSession(); //get the session from the sessionFactory
        Transaction transaction = session.beginTransaction();
        String re="";
        re+="<u>";
        for(String string:menu){
            System.out.println("menuitem="+string);
            if(string!=""&&string!=null){
                if(string.equals("<a href='Menu?redirection=selectCourse'>Course Catalogue</a>")){
                    re+="<li id='thrv-1'>"+string;
                    String getCategoryNameSQL = "SELECT C.category_name FROM Category C";
                    ArrayList<String> categories = (ArrayList) session.createSQLQuery(getCategoryNameSQL).list();
                    ArrayList<String>  allc= (ArrayList) session.createSQLQuery("SELECT p.cname FROM payment p WHERE p.ucode='"+((Gebruiker)sessie.getAttribute("cuser")).getuCode() +"' ").list();
                    allc.addAll((ArrayList) session.createSQLQuery("SELECT C.cname FROM course C WHERE C.teacher_ucode='"+((Gebruiker)sessie.getAttribute("cuser")).getuCode() +"'").list());
                    if(categories.size()>0&&allc.size()>0){
                    re+="<button class='hidek' id='thrk-1' onclick='hide(\"-1\")'>-</button><ul id='thr-1'>";
                    for (int i = 0; i < categories.size(); i++) {
                        
                    ArrayList<String>  coursesl= (ArrayList) session.createSQLQuery("SELECT p.cname FROM payment p WHERE p.ucode='"+((Gebruiker)sessie.getAttribute("cuser")).getuCode() +"' AND p.cname IN(SELECT C.cname FROM course C WHERE C.category_category_name='"+categories.get(i)+"')").list();
                    coursesl.addAll((ArrayList) session.createSQLQuery("SELECT C.cname FROM course C WHERE C.teacher_ucode='"+((Gebruiker)sessie.getAttribute("cuser")).getuCode() +"' AND C.category_category_name='"+categories.get(i)+"'").list());
                    removeDuplicateWithOrder(coursesl);
                    if(coursesl.size()>0){
                    re+="<li id='thrv"+i+"'>"+categories.get(i) +"<button class='hidek' id='thrk"+i+"' onclick='hide(\""+i+"\")'>-</button><ul id='thr"+i+"'>";
                    for (int j = 0; j < coursesl.size(); j++) {
                        re+="<li><A HREF='Menu?redirection=viewCourse&name="+coursesl.get(j)+"'>"+coursesl.get(j) +"</A></li>";
                    }
                    re+="</ul></li>";
                    }
                    }
                    re+="</ul>";
                }
                    re+="</li>";
                }else{
                    re+="<li>"+string+"</li>";
                }
            }
        }
        transaction.commit();
        re+="</u>";
        return re;
    }
    public String toSave(List<String> menu) {
        String re="";
        for(String string:menu){
            System.out.println("menuitem="+string);
            if(string!=""&&string!=null){
            re+=string+"<>";
            }
        }
        return re;
    }

    public void fillmenu() {
        if(sessie.getAttribute("menu")!=null){
        menu.clear();
        menu=(ArrayList<String>)sessie.getAttribute("menu");
        menuAdmin=(ArrayList<String>)sessie.getAttribute("Adminmenu");
        
        }
    }

    public void saveMenu() {
        if(menu.contains("<a href='Menu?redirection=logout'>logout</a>")){
        menu.remove("<a href='Menu?redirection=logout'>logout</a>");
        menu.add("<a href='Menu?redirection=logout'>logout</a>");
        }
         sessie.setAttribute("menu",menu);
         sessie.setAttribute("Adminmenu",menuAdmin);
    }

    public String get() {
        
        if(menu.contains("<a href='Menu?redirection=logout'>logout</a>")){
        menu.remove("<a href='Menu?redirection=logout'>logout</a>");
        menu.add("<a href='Menu?redirection=logout'>logout</a>");
        }
        
        String re="";
        if(menu.size()>0){
        re+="<div class='cleared'></div>\n</div>\n</div>\n<div class='art-blockheader'><div class='l'></div><div class='r'></div><h3 class='t'>Main Menu</h3>\n</div>\n<div class='art-blockcontent'>\n    <div class='art-blockcontent-body'>";
        re+=toString(menu);
        }
                
        if(menuAdmin.size()>0){
                re+="<div class='cleared'></div>\n</div>\n</div>\n<div class='art-blockheader'><div class='l'></div><div class='r'></div>\n<h3 class='t'>Admin Menu</h3>\n</div>\n<div class='art-blockcontent'>\n    <div class='art-blockcontent-body'>";
                re+=toString(menuAdmin);
        }
        return re;
    }

    void addAdminOptions() {
        
        System.out.println("blablabla\t\tblablabla");
        menuAddAdmin("manageHomepage", "Homepage Manager");
        menuAddAdmin("courseManager","Course Manager");
        menuAddAdmin("manageStudent","Student Manager");
        menuAddAdmin("manageTeacher","Teacher Manager");
        menuAddAdmin("send announcement","Send Announcement");
        
    }

    Object getList() {
        
        if(menu.contains("<a href='Menu?redirection=logout'>logout</a>")){
        menu.remove("<a href='Menu?redirection=logout'>logout</a>");
        menu.add("<a href='Menu?redirection=logout'>logout</a>");
        }
        String re="<a href='#'><span class='l'></span><span class='r'></span><span class='t'>Main menu</span></a>\n<ul>\n";
        for(String string:menu){
            System.out.println("menuitem="+string);
            if(string!=""&&string!=null){
            re+="<li>"+string+"</li>\n";
            }
        }
        re+="</ul>\n";
        if(menuAdmin.size()>0){
            re+="</li>\n<li>\n<a href='#'><span class='l'></span><span class='r'></span><span class='t'>Admin menu</span></a>\n<ul>\n";
        for(String string:menuAdmin){
            System.out.println("menuadminitem="+string);
            if(string!=""&&string!=null){
            re+="<li>"+string+"</li>\n";
            }
        }
        
        re+="</ul>\n";
        }
        return re;
    }
    public void removeDuplicateWithOrder(ArrayList arlList)
 {
 Set set = new HashSet();
 List newList = new ArrayList();
 for (Iterator iter = arlList.iterator();    iter.hasNext(); ) {
 Object element = iter.next();
   if (set.add(element))
      newList.add(element);
    }
    arlList.clear();
    arlList.addAll(newList);
}
}
